package com.caird.pmp.iot.handler;

import com.caird.common.utils.ClassUtils;
import com.caird.pmp.iot.Configration;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component("BbHandlerFactory")
public class HandlerFactory implements ApplicationRunner {


    private static Map<String, IHandler> hanlders = new HashMap<>();

    @Override
    public void run(ApplicationArguments args) {
        List<Class<? extends IHandler>> classList = ClassUtils.getAllClassByInterface(IHandler.class);
        for (Class<? extends IHandler> c : classList) {
            String key = c.getName().substring(c.getName().length()-4);
            if (!key.equals("Base"))
                hanlders.put(key, Configration.getApplicationContext().getBean(c));
        }
    }

    public static IHandler get(String id) {
        return hanlders.get(id);
    }


    public static IHandler get(short id) {
        return get(String.format("%04X", id));
    }
}
